using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Peradigm.Framework.Domain.Security;
using Peradigm.Framework.UI.Web;

using Liberty.Application.Security;
using Liberty.Application.Security.Exceptions;

[Serializable]
public partial class Security_LoginView : AbstractPage
{
    #region Constants
    public const string
        LIBERTY_USER = "Users";
    #endregion

    #region Event Handlers
    protected void cmdLogin_Click( object sender, EventArgs e )
    {
        SecurityController controller = new SecurityController();
        try
        {
            this.ApplicationUser = controller.AuthenticateUserWith( this.UserName, this.Password );
            this.AuthorizeAccess();
            this.NavigateToRequestedPage();
        }
        catch( LoginFailedException )
        {
            this.DisplayAuthenticationError();
        }
    }    
    #endregion

    #region Methods
    protected override void UpdateView()
    {
    }
    #endregion

    #region Properties
    #endregion

    #region Private Methods
    private void AuthorizeAccess()
    {
        if( this.ApplicationUser.IsInRole( LIBERTY_USER ) )
        {
            FormsAuthentication.SetAuthCookie( this.ApplicationUser.Name, false );
            Context.User = this.ApplicationUser;
        }
        else
        {
            throw new LoginFailedException( "User is not a IMSFF member. Please contact the Site Administrator." );
        }
    }

    private void DisplayAuthenticationError()
    {
        lblErrorMessage.Visible = true;
    }

    private void NavigateToRequestedPage()
    {
        string requestedPageURL = FormsAuthentication.GetRedirectUrl( this.User.Identity.Name, false );
        this.Server.Transfer( requestedPageURL );
    }
    #endregion

    #region Private Properties
    private User ApplicationUser
    {
        get
        {
            UserVariable userVariable = new UserVariable();
            return userVariable.User;
        }
        set
        {
            UserVariable userVariable = new UserVariable();
            userVariable.User = value;
        }
    }

    private string UserName
    {
        get
        {
            return txtUserName.Text;
        }
    }

    private string Password
    {
        get
        {
            return txtPassword.Text;
        }
    }
    #endregion

    #region Construction and Finalization
    #endregion

    #region Data Elements
    #endregion
}
